<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    const obj = {
      a: 3,
      b: {
        x: 10,
        y: 20
      },
      // c: {
      //   z: 666
      // }
      d: function() {
        console.log("ddd");
        
      }
    }
    // console.log(obj.b.x); // undefined
    // console.log(obj.c.z); // 报错
    // Cannot read properties of undefined (reading 'z')

    // ?.表示可选链操作符
    // 如果obj.c存在，那么obj.c?.z相当于就是obj.c.z
    // 如果obj.c不存在，那么obj.c?.z相当于就是obj.c
    console.log(obj.c?.z);
    /*
      obj.c?.z
      相当于是
      if (obj.c) {
        obj.c.z
      } else {
        obj.c 
      }
    */ 
    
    
    obj.d?.()
    
  </script>
</body>
</html>